import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import storageApi from '../../core/storage/storage';

/*eslint-disable */
@Inject('$log', '$scope', '$rootScope', '$state', 'homeStatsService', '$http', 'ngDialog')
class leaderHomeController {
  constructor() {
    this.data = {};

    this.param = {
      orgItem: null,
    }

    this.userName = $rootScope.userName ||  storageApi.get('userName');

    // 获取问题分类
    this.getProblemClassify();

    // 获取问题分布
    this.getProblemDistribution();

    this.queryStaffInOutStatistics();

    this.queryDeviceInOutStatistics();

    this.initChart2();

    $scope.$watch('vm.param.orgItem', (newValue) => {
      if (newValue)
        this.getUnitPatrol();
    });
  }

  // 获取单位巡查概况
  getUnitPatrol() {
    this.homeStatsService
      .queryOrgPatrol(this.param.orgItem.id)
      .then((res) => {
        if (res.success && res.data) {
          this.orgPatrolSituation = res.data;
        }
      });
  }

  // 获取人员进出场
  queryStaffInOutStatistics() {
    this.homeStatsService
      .queryStaffInOutStatistics({orgId: this.$rootScope.orgId || storageApi.get('orgId')})
      .then((res) => {
        if (res.success) {
          if (res.rows && res.rows.length > 0) {
            this.staffInOut = res.rows[res.rows.length - 1];
          }
        }
      });
  }

  // 获取设备进入场
  queryDeviceInOutStatistics() {
    this.homeStatsService
      .queryDeviceInOutStatistics({orgId: this.$rootScope.orgId || storageApi.get('orgId')})
      .then((res) => {
        if (res.success) {
          if (res.rows && res.rows.length > 0) {
            this.deviceInOut = res.rows[res.rows.length - 1];
          }
        }
      });
  }

  // 获取问题分类
  getProblemClassify() {
    let name = [], series = [];
    this.homeStatsService
      .queryProblemClassify()
      .then((res) => {
        if (res.success && res.data) {
          if (res.data.length > 6) {
            this.problemData = res.data.splice(0, 6);
          } else {
            this.problemData = res.data;
          }
          this.problemData.forEach((item) => {
            name.push(item.name);
            series.push({value: item.total, name: item.name});
          })
          this.initChart(name, series);
        }
      });
  }

  // 获取问题分布
  getProblemDistribution() {
    this.homeStatsService
      .queryProblemDistribute()
      .then((res) => {
        if (res.success && res.data) {
          this.distributeData = res.data;
        }
      });
  }

  initChart(name, series) {
    const option = {
      tooltip: {
        trigger: 'item',
        formatter: "{a} <br/>{b}: {c} ({d}%)"
      },
      legend: {
        orient: 'vertical',
        x: 'left',
        data: name,
      },
      series: [
        {
        name:'问题分类',
        type:'pie',
        radius: ['50%', '70%'],
        center: ['70%', '50%'],
        avoidLabelOverlap: false,
        label: {
          normal: {
            show: false,
            position: 'center'
          },
          emphasis: {
            show: true,
            textStyle: {
              fontSize: '18',
              fontWeight: 'bold'
            }
          }
        },
        labelLine: {
          normal: {
            show: false
          }
        },
        data: series,
        }
      ]
    };
    const myChart = echarts.init(document.getElementById('leader-home-problem-classify'));
    myChart.setOption(option);
  }

  initChart2() {
    const option = {
      tooltip : {
      formatter: "{a} <br/>{b} : {c}%"
      },
      series: [
        {
          name: '业务指标',
          type: 'gauge',
          radius:'100%',
          detail: {formatter:'{value}'},
          data: [{value: 50, name: ''}]
        }
      ]
    };
    const myChart = echarts.init(document.getElementById('leader-home-total-number'));
    myChart.setOption(option);
  }

  logout() {
    this
      .$http
      .get('/api/v1/logout')
      .then((res) => {
        if (res.success) {
          this.$state.go('login');
          this.$rootScope.isLogined = false;
          // this.$interval.cancel(this.$rootScope.msgInterval);
        }
      });
  }

  changePwd() {
    this.ngDialog.open({
      template: 'leaderHomePwdDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: '$leaderHomePwdDialog',
      controller($rootScope, $http, $scope, $log, $state, loginService, ngDialogService) {
        'ngInject';

        this.userInfo = {
          loginName: $rootScope.userName || storageApi.get('userName'),
        };

        this.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;
          // 表单校验
          if (param.$invalid) {
            return false;
          } else if (param.confirmPassWord.$viewValue !== param.newPassWord.$viewValue) {
            ngDialogService.alertError('确认密码和新密码不一致');
            return false;
          } else {
            loginService.update(this.userInfo)
              .then((res) => {
                if (res.success) {
                  ngDialogService.alertSuccess(res.message);
                  $scope.closeThisDialog();
                  $state.go('login');
                } else {
                  ngDialogService.alertError(res.message);
                }
              });
          }
        };
      },
    });
  }

}

export default leaderHomeController;
